package com.tmall.testgen.plugin.wizards;

import java.io.File;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.client.genfile.TotalGenHandler;
import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.common.bo.interact.back.DBInfo;
import com.tmall.testgen.plugin.Activator;
import com.tmall.testgen.plugin.ConsoleFactory;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class GenTestCodeWizadPage extends WizardPage {

	private Text urlText;
	private Text userText;
	private Text pwText;
	private Text driverClassText;
	private Text sqlSnap;
	//private Text testClassNameText;

	// private Composite radioGroup;
	private Combo dataSourceCombo;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public GenTestCodeWizadPage(ISelection selection) {
		super("GenTestCodeWizadPage");
		setTitle("配置界面");
		setDescription("测试代码生成配置界面");

		// this.selection = selection;
	}

	private List<DBInfo> dbInfosList;
	private Composite container;
	private Button sqlBtn;
	private Composite comSql;

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		// CreateTestCodeWizadPage prevPage = (CreateTestCodeWizadPage)
		// getPreviousPage();
		// ConnectionSuccResult result = prevPage.getResult();
		container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 4;
		layout.makeColumnsEqualWidth = false;
		container.setLayout(layout);
		// class name
		Label classNamelabel = new Label(container, SWT.NONE);
		classNamelabel.setText("ClassName:");
		GridData classNamelabelGd = new GridData(SWT.RIGHT, SWT.FILL, false,false, 1, 1);
		classNamelabel.setLayoutData(classNamelabelGd);
		// test class path Text
		final Text testClassNameText = new Text(container, SWT.BORDER);
		GridData testClassNameTextGd = new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1);
		testClassNameText.setLayoutData(testClassNameTextGd);
		testClassNameText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
			    ClientDataCenter.getInstance().setTestName(testClassNameText.getText());
			}
		});
		
		//======
		Label projectNamelabel = new Label(container, SWT.NONE);
		projectNamelabel.setText("Project:");
        GridData projectNamelabelGd = new GridData(SWT.RIGHT, SWT.FILL, false,false, 1, 1);
        projectNamelabel.setLayoutData(projectNamelabelGd);
        // test class path Text
        
        
        String projectInfo="";
        
        String targetProjectName=TargetProjectConfig.getInstance().getTargetProjectName();
        String targetProjectNameStore= Activator.getDefault().getConfig(targetProjectName);
        if(targetProjectNameStore!=null&&targetProjectNameStore.length()>0)
            projectInfo=targetProjectNameStore;
        else{
            projectInfo=TargetProjectConfig.getInstance().getTargetProjectName()+"-test$"+"src";
        }
        
        ClientDataCenter.getInstance().setTestProjectInfo(projectInfo);
            
        
        
        final Text projectNameText = new Text(container, SWT.BORDER);
        projectNameText.setText(projectInfo);
        GridData projectNameTextGd = new GridData(SWT.FILL, SWT.FILL, true,false, 1, 1);
        
        projectNameText.setLayoutData(projectNameTextGd);
        projectNameText.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                ClientDataCenter.getInstance().setTestProjectInfo(projectNameText.getText());
                Activator.getDefault().setConfig(TargetProjectConfig.getInstance().getTargetProjectName(), projectNameText.getText());
            }
        });
        
		
		// sql button
		sqlBtn = new Button(container, SWT.PUSH);
		sqlBtn.setText("设置sql快照");
		GridData sqlBtntGd = new GridData(SWT.LEFT, SWT.FILL, true, false, 4, 1);
		sqlBtn.setLayoutData(sqlBtntGd);

		sqlBtn.addSelectionListener(sqlBtnListener);

		GridData comSqltGd = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 10);
		comSql = new Composite(container, SWT.NONE | SWT.BORDER);
		comSql.setLayoutData(comSqltGd);
		comSql.setLayout(layout);
		// datasource label
		Label dataSourceLabel = new Label(comSql, SWT.NONE);
		dataSourceLabel.setText("数据源:");
		GridData dataSourceLabelGd = new GridData(SWT.RIGHT, SWT.FILL, false,
				false, 1, 1);
		dataSourceLabel.setLayoutData(dataSourceLabelGd);

		dataSourceCombo = new Combo(comSql, SWT.NONE | SWT.BORDER);
		GridData comGd = new GridData(SWT.LEFT, SWT.FILL, false, false, 3, 1);
		dataSourceCombo.setLayoutData(comGd);
		dataSourceCombo.setItems(new String[] { "自定义数据源" });
		dataSourceCombo.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dataSourceModifyDeal();
			}
		});

		// url
		Label urlLabel = new Label(comSql, SWT.NONE);
		urlLabel.setText("url:");
		GridData urlLabelGd = new GridData(SWT.RIGHT, SWT.FILL, false, false,1, 1);
		urlLabel.setLayoutData(urlLabelGd);

		urlText = new Text(comSql, SWT.NONE | SWT.BORDER);
		GridData urlTextGd = new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1);
		urlText.setLayoutData(urlTextGd);

		// user
		Label userLabel = new Label(comSql, SWT.NONE);
		userLabel.setText("用户名:");
		GridData userLabelGd = new GridData(SWT.RIGHT, SWT.FILL, false, false,1, 1);
		userLabel.setLayoutData(userLabelGd);

		userText = new Text(comSql, SWT.NONE | SWT.BORDER);
		GridData userTextGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1,1);
		userText.setLayoutData(userTextGd);

		// password
		Label pwLabel = new Label(comSql, SWT.NONE);
		pwLabel.setText("密码:");
		GridData pwLabelGd = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1,1);
		pwLabel.setLayoutData(pwLabelGd);

		pwText = new Text(comSql, SWT.NONE | SWT.BORDER);
		GridData pwTextGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		pwText.setLayoutData(pwTextGd);

		// driver class
		Label driverClasslabel = new Label(comSql, SWT.NONE);
		driverClasslabel.setText("driverClass:");
		GridData driverClasslabelGd = new GridData(SWT.RIGHT, SWT.FILL, false,
				false, 1, 1);
		driverClasslabel.setLayoutData(driverClasslabelGd);

		// test driver class path
		driverClassText = new Text(comSql, SWT.BORDER);
		GridData driverClassTextGd = new GridData(SWT.FILL, SWT.FILL, true,
				false, 3, 1);
		driverClassText.setLayoutData(driverClassTextGd);

		Label sqlLabel = new Label(comSql, SWT.NONE);
		sqlLabel.setText("sql快照:");
		GridData sqlLabelGd = new GridData(SWT.RIGHT, SWT.FILL, false, false,1, 3);
		sqlLabel.setLayoutData(sqlLabelGd);

		GridData sqlGd = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 3);
		sqlSnap = new Text(comSql, SWT.BORDER | SWT.MULTI);
		sqlSnap.setLayoutData(sqlGd);
		comSql.setVisible(false);
		setControl(container);
	}

	private SelectionListener sqlBtnListener = new SelectionListener() {

		@Override
		public void widgetSelected(SelectionEvent e) {
			if (!comSql.getVisible()) {
				comSql.setVisible(true);
				sqlBtn.setText("收起设置  <<");
				container.layout();
			} else {
				comSql.setVisible(false);
				sqlBtn.setText("设置sql快照");
				container.layout();
			}
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {

		}

	};

	private void collectParam() {
		DBInfo dbInfo = new DBInfo();
		dbInfo.setDriverClass(getDriverClassText().getText());
		dbInfo.setPassword(getPwText().getText());
		dbInfo.setUrl(getUrlText().getText());
		dbInfo.setUserName(getUserText().getText());

		ClientDataCenter.getInstance().setDbInfo(dbInfo);
		ClientDataCenter.getInstance().setSqlSnap(getSqlSnap().getText());

	}

	public boolean beginGenCode(boolean needClear) throws Exception {
	    String testName=ClientDataCenter.getInstance().getTestName();
		if (testName==null||testName.length() == 0) {
			MyMessageDialog.openWarnDialog(getShell(), "无类名不Java！！");
			return false;
		}
		

		collectParam();
		TotalGenHandler.genJavaFile(TargetProjectConfig.getInstance().getTargetProjectPath(), 
		                                        ClientDataCenter.getInstance().getTestName(),
		                                        ClientDataCenter.getInstance().getInfoes());

		

		if (needClear) {
			TargetProjectConfig.clear();
			ClientDataCenter.clear();
			RunInSpringHandler.getInstance().clear();
		}
		return true;
	}


	public void initDataInfoList(List<DBInfo> dbInfoes) {
		if (dbInfoes == null)
			return;

		dbInfosList = dbInfoes;

		int len = 0;
		if (dbInfosList != null) {
			len = dbInfosList.size();
		}
		String items[] = new String[len + 1];
		for (int i = 0; i < len; i++) {
			items[i] = "数据源" + (i + 1);
		}
		items[len] = "自定义数据源";
		dataSourceCombo.setItems(items);
		
		if(dbInfosList!=null&&dbInfosList.size()>0){
			dataSourceCombo.select(0);
			DBInfo dbInfo = dbInfosList.get(0);
			urlText.setText(dbInfo.getUrl());
			userText.setText(dbInfo.getUserName());
			pwText.setText(dbInfo.getPassword());
			driverClassText.setText(dbInfo.getDriverClass());
		}
	}

	private void dataSourceModifyDeal() {
		int index = dataSourceCombo.getSelectionIndex();
		if (index < 0 || index >= dbInfosList.size()) {
			urlText.setText("");
			userText.setText("");
			pwText.setText("");
			driverClassText.setText("");
			return;
		}
		DBInfo dbInfo = dbInfosList.get(index);
		urlText.setText(dbInfo.getUrl());
		userText.setText(dbInfo.getUserName());
		pwText.setText(dbInfo.getPassword());
		driverClassText.setText(dbInfo.getDriverClass());

		// ClientDataCenter.getInstance().setDbInfo(dbInfo);

	}

	public Text getUrlText() {
		return urlText;
	}

	public void setUrlText(Text urlText) {
		this.urlText = urlText;
	}

	public Text getSqlSnap() {
		return sqlSnap;
	}

	public Text getUserText() {
		return userText;
	}

	public Text getPwText() {
		return pwText;
	}

	
	public Text getDriverClassText() {
		return driverClassText;
	}

}